package sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author XY
 * @version 1.0
 * @date 2022/3/22 9:24
 * @Description
 */
public class Leet15 {
    public static List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        int i=0,j=nums.length-1;
        List<List<Integer>> res = new ArrayList<>();
        while(i<=j){
            if(nums[i]+nums[i+1]+nums[j]>0){
                j--;
                continue;
            }
            for (int n = i+1; n < j; n++) {
                List<Integer> list = new ArrayList<>();
                if(nums[i]+nums[j]+nums[n]>0){
                    break;
                }else if(nums[i]+nums[j]+nums[n]==0){
                    list.add(nums[i]);
                    list.add(nums[n]);
                    list.add(nums[j]);
                    if(!res.contains(list)){
                        res.add(list);
                    }
                }
            }
            i++;

        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr= {-2,0,1,1,2};
        threeSum(arr);
    }
}
